package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderDetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderDetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    private int a;
    private int orderid;

    @Override
    public int createOrders(String userId, Integer businessId, Integer daId, Double orderTotal) {
        CartDao cartDao = new CartDaoImpl();
        OrdersDao dao = new OrdersDaoImpl();
        OrderDetailetDao oddao = new OrderDetailetDaoImpl();
        try {
            // 开启事务
            DBUtil.beginTransaction();
            // 获取连接
            DBUtil.getConnection();
            // 向orders表中添加一条数据 并返回orderId
            orderid = dao.insertOrders(userId, businessId, daId, orderTotal);
            // 利用传进来的userId和businessId对购物车进行查询
            // 得到一个购物车list集合
            List<Cart> cartList = cartDao.listCart(userId, businessId);
            // 将得到的购物车list集合中的数据添加到订单明细表中
            oddao.insertOrderDetailet(cartList);
            List<OrderDetailet> odList = new ArrayList<>();
            // 购物车中有几个cart对象就进行几次循环操作
            for (int i = 0;i<cartList.size();i++) {
                OrderDetailet orderDetailet = new OrderDetailet();
                // 将获取到的数据添加到orderDetailet对象中
                orderDetailet.setOrderId(orderid);
                orderDetailet.setFoodId(cartList.get(i).getFoodId());
                orderDetailet.setQuantity(cartList.get(i).getQuantity());
                // 将orderDetailet对象存入订单明细list集合中
                odList.add(orderDetailet);
                // 根据传进来的userId和businessId对购物车进行删除
                cartDao.removeCart(userId,businessId,cartList.get(i).getFoodId());
            }
            // 提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                // 有异常时回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orderid;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders order = new Orders();
        OrdersDao dao = new OrdersDaoImpl();
        OrderDetailetDao oddao = new OrderDetailetDaoImpl();
        try {
            // 根据orderId得到一个order对象
            order = dao.getOrdersById(orderId);
            // 根据orderId查询订单明细 得到一个订单明细lisst集合
            List<OrderDetailet> list = oddao.getOrderDetailetById(orderId);
            order.setList(list);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return order;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        List<Orders> list = new ArrayList<>();
        OrdersDao dao = new OrdersDaoImpl();
        OrderDetailetDao oddao = new OrderDetailetDaoImpl();
        try {
            // 根据userId得到一个orders集合
            list = dao.listOrdersByUserId(userId);
            for (Orders orders : list) { // 对orderList中的order进行循环得到orderId
                // 利用得到的orderId查询orderDetailet得到订单明细list集合中
                List<OrderDetailet> list1 = oddao.getOrderDetailetById(orders.getOrderId());
                orders.setList(list1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
}
